/*
* @Author: yongqiang.sun
* @Date:   2018-01-17 23:24:47
* @Last Modified by:   yongqiang.sun
* @Last Modified time: 2018-01-18 01:33:34
*/
(function (window) {
    var MESSAGE_TEMPLATE = [
'<div class="message">',
    '<div class="message-inner">',
        '<div class="message-header message-no-header">',
            '<div class="message-title">',

            '</div>',
        '</div>',
        '<div class="message-body">',
        '{{content}}',
        '</div>',
        '<div class="message-footer">',
            '<button class="btn btn-primary btn-large btn-ok">确定</button>',
        '</div>',
    '</div>',
'</div>'
    ].join('')
    var CONFIRM_TEMPLATE = [
'<div class="message">',
    '<div class="message-inner">',
        '<div class="message-header message-no-header">',
            '<div class="message-title">',

            '</div>',
        '</div>',
        '<div class="message-body">',
        '{{content}}',
        '</div>',
        '<div class="message-footer message-confirm">',
            '<button class="btn btn-default btn-cancel">取消</button>',
            '<button class="btn btn-primary btn-ok">确定</button>',
        '</div>',
    '</div>',
'</div>'
    ].join('')
    var TOAST_TEMPLATE = [
'<div class="message message-toast">',
    '<div class="message-inner">',
        '{{content}}',
    '</div>',
'</div>'
    ].join('')
    var $ = window.Zepto || window.jQuery;
    var Message = function (option) {
        var defaults = {
            $el: $('body'),
            timeout: 2000
        }
        this.$message = null;
        this.option = $.extend(defaults, option);
        if (!(this instanceof Message)) {
            return new Message(option);
        }
    }

    Message.prototype = {
        show: function (text) {
            var self = this;
            this.$message = $(MESSAGE_TEMPLATE.replace(/{{content}}/ig, text)).appendTo(this.option.$el).find('.btn-ok').click(function () {
                if (self.option.onConfirmClick) {
                    var result = self.option.onConfirmClick();
                    if (result === false) {
                        return;
                    }
                }
                self.hide()
            }).end();
            return this;
        },
        hide: function () {
            this.$message.remove()
        },
        confirm: function (text) {
            var self = this;
            this.$message = $(CONFIRM_TEMPLATE.replace(/{{content}}/ig, text)).appendTo(this.option.$el).find('.btn-ok').click(function () {
                if (self.option.onConfirmClick) {
                    var result = self.option.onConfirmClick();
                    if (result === false) {
                        return;
                    }
                }
                self.hide()
            }).end().find('.btn-cancel').click(function () {
                if (self.option.onCancelClick) {
                    var result = self.option.onCancelClick();
                    if (result === false) {
                        return;
                    }
                }
                self.hide()
            }).end();
            return this;
        },
        toast: function (text) {
            var self = this;
            this.$message = $(TOAST_TEMPLATE.replace(/{{content}}/ig, text)).appendTo(this.option.$el);
            if (this.option.timeout > 0) {
                setTimeout(function () {
                    self.hide();
                }, this.option.timeout);
            }
            return this;
        }
    }

    $.message = Message;
})(window)
